import crtkc from "./crtkc";
import { User } from "../scripts/UserManager";
import { UserData } from "../scripts/UserConst";
import { Tip } from "../scripts/TipManager";
import { Event } from "../scripts/EventManager";
import { default as GameEvent } from "../scripts/EventConst";
const {ccclass, property} = cc._decorator;

@ccclass
export default class deskView extends cc.Component {

    @property(cc.Label)
    lab_desk: cc.Label = null;

    @property(cc.Node)
    node_btn: cc.Node = null;



    start () {
        const deskCanUse = crtkc.getData('shortcutCanUse', 0);
        if(deskCanUse == 1){//可以添加
            this.lab_desk.string = '添加';
        }else if(deskCanUse == 2){//添加过桌面快捷方式
            let hasGet = crtkc.getData('desk', { today: '', canGet: '0' });
            let date = new Date();
            let today = date.getUTCFullYear() + "-" + date.getUTCMonth() + "-" + date.getUTCDay();
            if(hasGet.today == today){
                this.lab_desk.string = '已领取';
            }else if(hasGet.canGet == 1){
                this.lab_desk.string = '领取奖励';
            }else{
                this.node_btn.active = false;
            }
        }else if(deskCanUse == 3){//点击添加桌面成功后,没退继续点开这个弹窗..
            this.node_btn.active = false;
        }
    }

    private close(){
        this.node.destroy();
    }

    private clickBtn(){
        const deskCanUse = crtkc.getData('shortcutCanUse', 0);
        if(deskCanUse == 1){//添加桌面
            //@ts-ignore
            window.__prv.addShortCut({
                sucCall: ()=>{
                    console.error("桌面快捷方式添加成功");
                    this.node_btn.active = false;
                    Tip.show("桌面快捷方式添加成功");
                    crtkc.saveData('shortcutCanUse', 3);
                    this.node.destroy();
                },
                failCall: ()=>{
                    console.error("桌面快捷方式添加失败");
                    Tip.show("桌面快捷方式添加失败");
                }
            });
        }else if(deskCanUse == 2){
            let hasGet = crtkc.getData('desk', { today: '', canGet: '0' });
            let date = new Date();
            let today = date.getUTCFullYear() + "-" + date.getUTCMonth() + "-" + date.getUTCDay();
            if(hasGet.today == today){
                console.error("已经领取");
            }else if(hasGet.canGet == 1){
                hasGet.today = today;
                crtkc.saveData('desk', hasGet);
                this.lab_desk.string = "已领取";
                // // 增加体力值的逻辑
                // let power = User.get(UserData.POWER);
                // let t = 1; // 这里的t值需要根据实际情况调整
                // User.set(UserData.POWER, power + 10 * t);
                // Tip.show("体力+" + 10 * t);
                // Event.emit(GameEvent.POWER_EFFECT, true);
                //增加钥匙逻辑
                let key = User.get(UserData.KEY);
                User.set(UserData.KEY, key + 1);
                Tip.show("钥匙+" + 1);
                Event.emit(GameEvent.KEY_EFFECT, true);
            }
        }
    }

}
